package gov.pbc.nn.kjgl.jfcr.handler;

import gov.pbc.nn.core.dao.PbcnnCoreHibernateDao;
import gov.pbc.nn.core.utils.SecurityUtils;
import gov.pbc.nn.kjgl.Constants;
import gov.pbc.nn.kjgl.entity.ComputerRoomApplyEntry;
import gov.pbc.nn.kjgl.jfcr.dao.ApplyEntryDao;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.bstek.uflo.env.Context;
import com.bstek.uflo.model.ProcessInstance;
import com.bstek.uflo.model.task.Task;
import com.bstek.uflo.process.assign.Assignee;
import com.bstek.uflo.process.listener.TaskListener;
import com.bstek.uflo.process.node.TaskNode;
import com.bstek.uflo.service.ProcessService;
import com.bstek.uflo.service.TaskService;

/**
 * 根据任务处理人修改流程节点名字
 * 
 * @author jack.zhao
 * 
 */
@Component("fcr.TaskName")
public class JfglTaskName implements TaskListener {
	
	@Autowired
	private ApplyEntryDao applyEntryDao;
	
//	@Resource(name = PbcnnCoreHibernateDao.BEAN_ID)
//	private PbcnnCoreHibernateDao coreHibernateDao;

	@Override
	public boolean beforeTaskCreate(Context context, ProcessInstance processInstance, TaskNode taskNode) {
		return false;
	}

	@Override
	public void onTaskComplete(Context context, Task task) {

	}

	@Override
	public void onTaskCreate(Context context, Task task) {
		String businessId = task.getBusinessId();
		ComputerRoomApplyEntry computerRoomApplyEntry = this.applyEntryDao.get(businessId, ComputerRoomApplyEntry.class);
		String assignee = task.getAssignee();
		boolean kz = SecurityUtils.containsRole(assignee, Constants.ROLE_KZ);
		boolean cz = SecurityUtils.containsRole(assignee, Constants.ROLE_CZ);
		boolean jg = SecurityUtils.containsRole(assignee, Constants.ROLE_JFGLY);
		if (kz) {
			task.setTaskName("科长审核【"+computerRoomApplyEntry.getTitle()+"】");
		} else if (cz) {
//			task.setTaskName("处长审核进入机房申请");
			task.setTaskName("处长审核【"+computerRoomApplyEntry.getTitle()+"】");
		} else if (jg) {
//			task.setTaskName("机房管理员审核进入机房申请");
			task.setTaskName("机房管理员审核【"+computerRoomApplyEntry.getTitle()+"】");
		}
		context.getSession().saveOrUpdate(task);
	}

}
